package com.mtask.web.service.impl;

import com.mtask.common.dto.TaskGroupResponseDTO;
import com.mtask.common.entity.task.TaskGroup;
import com.mtask.common.entity.task.TaskProject;
import com.mtask.common.dto.TaskGroupDTO;
import com.mtask.web.repository.TaskGroupRepository;
import com.mtask.web.repository.TaskProjectRepository;
import com.mtask.web.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class GroupServiceImpl implements GroupService {
  @Resource private TaskGroupRepository taskGroupRepository;

  @Autowired private TaskProjectRepository taskProjectRepository;

  @Override
  public void save(TaskGroupDTO dto) {
    TaskGroup taskGroup;
    if (dto.getIsUpdate()) {
      taskGroup =
          taskGroupRepository
              .findById(dto.getTaskGroupId())
              .orElseThrow(() -> new RuntimeException("参数不合法"));
      taskGroup.setGroupName(dto.getGroupName());
    } else {
      TaskProject taskProject =
          taskProjectRepository
              .findById(dto.getTaskProjectId())
              .orElseThrow(() -> new RuntimeException("参数不合法"));
      taskGroup = new TaskGroup(dto.getGroupName(), taskProject);
    }
    taskGroupRepository.save(taskGroup);
  }

  @Override
  public Page<TaskGroupResponseDTO> findAll(Pageable pageable) {
    Page<TaskGroup> taskGroups = taskGroupRepository.findAll(pageable);
    List<TaskGroupResponseDTO> content =
        taskGroups.getContent().stream()
            .map(
                taskGroup ->
                    TaskGroupResponseDTO.builder()
                        .groupName(taskGroup.getGroupName())
                        .id(taskGroup.getId())
                        .projectName(taskGroup.getTaskProject().getName())
                        .projectId(taskGroup.getTaskProject().getId())
                        .build())
            .collect(Collectors.toList());
    return new PageImpl<>(content, taskGroups.getPageable(), taskGroups.getTotalElements());
  }

  @Override
  @Transactional
  public void deleteById(Integer id) {
    taskGroupRepository.customDeleteById(id);
  }

  @Override
  public List<TaskGroupResponseDTO> findByProjectId(Integer id) {
    Optional<TaskProject> taskProject = taskProjectRepository.findById(id);
    return taskProject
        .map(
            project ->
                project.getTaskGroups().stream()
                    .map(
                        taskGroup ->
                            TaskGroupResponseDTO.builder()
                                .id(taskGroup.getId())
                                .groupName(taskGroup.getGroupName())
                                .build())
                    .collect(Collectors.toList()))
        .orElseGet(ArrayList::new);
  }
}
